package com.java.system.controller;

import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.system.entity.Role;
import com.java.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 带条件分页查询角色
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = roleService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody Role roleDto) {
        roleService.updateOrSave(roleDto);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        roleService.removeById(id);
        return R.ok();
    }

    /**
     * 保存资源
     */
    @PostMapping("/save-resource")
    public R saveResource(@RequestBody Role roleDto) {
        log.info("保存角色资源关联开始");
        roleService.saveResource(roleDto);
        return R.ok();
    }

    /**
     * 加载已关联的资源
     */
    @GetMapping("/list-resource/{roleId}")
    public R listResource(@PathVariable String roleId) {
        log.info("加载资源开始");
        List<String> resourceIdList = roleService.listResource(roleId);
        return R.ok(resourceIdList);
    }

    /**
     * 保存用户
     */
    @PostMapping("/save-user")
    public R saveUser(@RequestBody Role roleDto) {
        log.info("保存角色用户关联开始");
        roleService.saveUser(roleDto);
        return R.ok();
    }

    /**
     * 加载用户
     */
    @GetMapping("/list-user/{roleId}")
    public R listUser(@PathVariable String roleId) {
        log.info("加载用户开始");
        List<String> userIdList = roleService.listUser(roleId);
        return R.ok(userIdList);
    }
}
